/* $Id: I18N.java 39 2009-07-24 22:48:06Z h_m_r@gmx.de $ */
package util;


import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class I18N {
	private static ResourceBundle messages = null;
	private static Locale locale = Locale.getDefault();
	private static String bundleName = "Messages";

	public static void setBundleName(String bundleName) {
		setResourceBundle(bundleName, locale);
	}

	public static void setLocale(Locale locale) {
		setResourceBundle(bundleName, locale);
	}

	public static void setResourceBundle(String bundleName, Locale locale) {
		I18N.bundleName = bundleName;
		I18N.locale = locale;
		messages = ResourceBundle.getBundle(bundleName, locale);
	}

	public static String __(String key) {
		try {
			return messages.getString(key);
		}
		catch(MissingResourceException mre) {
		}
		return "__missing i18n: "+key+"__";
	}

	public static String __(String key, Object[] args) {
		MessageFormat formatter = new MessageFormat("");
		formatter.setLocale(locale);
		formatter.applyPattern(__(key));
		return formatter.format(args);
	}
}
